﻿using DomainService.IService;
using Microsoft.AspNetCore.Http;
using Model.BusinessModel.OutPut;
using Model.OverallAtuhModel;
using Utility;
using Utility.Enum;

namespace DomainService.Service
{
    /// <summary>
    /// 基础服务
    /// </summary>
    public class BaseService : IBaseService
    {
        /// <summary>
        /// 上下文
        /// </summary>
        private readonly IHttpContextAccessor _httpContextAccessor;

        /// <summary>
        /// 上下文
        /// </summary>
        /// <param name="httpContextAccessor"></param>
        public BaseService(IHttpContextAccessor httpContextAccessor)
        {
            _httpContextAccessor = httpContextAccessor;
        }

        /// <summary>
        /// 获取当前页面菜单id
        /// </summary>
        /// <returns></returns>
        public string GetCurrentMenuId()
        {
            var menuId = _httpContextAccessor.HttpContext.Request.Headers["menuId"].ToString();
            return menuId;
        }

        /// <summary>
        /// 获取登录人员信息
        /// </summary>
        /// <returns></returns>
        public LoginOutPut GetUserInfo()
        {
            LoginOutPut loginResult = new() { CorporationKey = "", SystemKey = "", ExpiresDate = "", Password = "", Token = "", UserId = "", UserName = "", RoleIds = "", DepartmentId = "" };
            if (_httpContextAccessor.HttpContext == null)
                return loginResult;
            var userInfo = _httpContextAccessor.HttpContext.User;

            if (userInfo.Identity.IsAuthenticated)
            {
                // 这里你可以获取用户的claims
                var claims = userInfo.Claims.ToList();
                loginResult.UserName = claims.FirstOrDefault(f => f.Type == "UserName").Value;
                loginResult.UserId = Convert.ToString(claims.FirstOrDefault(f => f.Type == "UserId").Value);
                loginResult.CorporationKey = Convert.ToString(claims.FirstOrDefault(f => f.Type == "CorporationKey").Value);
                loginResult.SystemKey = Convert.ToString(claims.FirstOrDefault(f => f.Type == "SystemKey").Value);
                loginResult.RoleIds = Convert.ToString(claims.FirstOrDefault(f => f.Type == "RoleIds").Value);
                loginResult.DepartmentId = Convert.ToString(claims.FirstOrDefault(f => f.Type == "DepartmentId").Value);
            }
            return loginResult;
        }

        /// <summary>
        /// 获取所有条件等式下拉项
        /// </summary>
        /// <returns></returns>
        public List<SelectOutPut> GetAllConditionalEquationList()
        {
            List<SelectOutPut> layuiSelectList = new()
            {
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.InExamine), value =((int)FlowApplyStateEnum.InExamine).ToString() },
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.AuditPass), value =((int)FlowApplyStateEnum.AuditPass).ToString() },
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.AuditRefuse), value =((int)FlowApplyStateEnum.AuditRefuse).ToString() },
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.AuditDismissal), value =((int)FlowApplyStateEnum.AuditDismissal).ToString() },
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.Cancel), value =((int)FlowApplyStateEnum.Cancel).ToString() },
                new SelectOutPut { label = EnumHelper.GetDescription( FlowApplyStateEnum.Filing), value =((int)FlowApplyStateEnum.Filing).ToString() },
            };
            return layuiSelectList;
        }

    }
}
